package com.apac.controllers;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes("user")
public class SessionController {

	@RequestMapping(value="/",method=RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request){
		
		ModelAndView modelAndView = new ModelAndView("Home");
		
		return modelAndView;
		
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public ModelAndView login(){
		return new ModelAndView("Login");
	}
	
	@RequestMapping(value="/error",method=RequestMethod.GET)
	public ModelAndView error(){
		return new ModelAndView("Error");
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public void logout(SessionStatus status,HttpSession session,HttpServletRequest request,HttpServletResponse response) throws IOException{
		session.invalidate();
		status.setComplete();
		
		response.sendRedirect(request.getServletContext().getContextPath());
	}
	
}
